package com.hxm.demo.huawei;

import java.util.Scanner;

/**
 * @author huangxiaoming
 * @date 2022/6/19
 **/
public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            int total = in.nextInt();
            String[] split = s.split(",");

            int[] prices = new int[split.length];
            boolean flag = true;
            for (int i = 0; i < split.length; i++) {
                if (Integer.parseInt(split[i]) <= total) {
                    flag = false;
                }
                prices[i] = Integer.parseInt(split[i]);
            }

            if (flag) {
                System.out.println(-1);
            }

            int[][] dp = new int[split.length + 1][total + 1];

            for (int i = 1; i <= split.length; i++) {
                for (int j = 1; j <= total; j++) {
                    dp[i][j] = dp[i - 1][j];
                    if (j >= prices[i - 1]) {
                        dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - prices[i - 1]] + prices[i - 1]);
                    }
                }
            }
            System.out.println(dp[split.length][total]);
        }
    }
}
